package com.baizhi.utils.guava;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import org.junit.Test;

import java.util.Set;

public class MultisetTest {

    /**
     * Multiset，支持重复的元素
     * 底层通过HashMap实现<key,count>
     */
    @Test
    public void test1() {
        Multiset<String> multiset = HashMultiset.create();
        multiset.add("a");
        multiset.add("b");
        multiset.add("c");
        multiset.add("a");
        // [a x 2, b, c]
        System.out.println(multiset);

        Set<Multiset.Entry<String>> entries = multiset.entrySet();
        for (Multiset.Entry<String> entry : entries) {
            System.out.println("元素：" + entry.getElement() + "，个数：" + entry.getCount());
        }

        Set<String> elements = multiset.elementSet();
        for (String element : elements) {
            System.out.println("集合里面的元素：" + element);
        }
    }
}
